﻿// <copyright file="KnownColorPicker.xaml.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-06-13</date>
// <summary>OpenWPFChart library.
// KnownColorPicker User Control.
// </summary>
// <revision>$Id$</revision>

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace OpenWPFChart.Helpers.ChooseColorDialog
{
	/// <summary>
	/// KnownColorPicker User Control.
	/// </summary>
	public partial class KnownColorPicker : UserControl
	{
		KnownColorPickerViewModel viewModel;
		/// <summary>
		/// Initializes a new instance of the <see cref="KnownColorPicker"/> class.
		/// </summary>
		public KnownColorPicker()
		{
			InitializeComponent();

			viewModel = new KnownColorPickerViewModel();
			viewModel.PropertyChanged += viewModel_PropertyChanged;
			contentHost.DataContext = viewModel;
		}

		/// <summary>
		/// Handles the PropertyChanged event of the viewModel object.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> 
		/// instance containing the event data.</param>
		void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "SelectedColor")
				SelectedColor = viewModel.SelectedColor;
		}

		#region SelectedColor
		/// <summary>
		/// SelectedColor backing DependencyProperty.
		/// </summary>
		private static readonly DependencyProperty SelectedColorProperty
			= DependencyProperty.Register("SelectedColor", typeof(Color), typeof(KnownColorPicker),
				new FrameworkPropertyMetadata(Colors.Transparent
					, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
					, SelectedColorPropertyChanged));
		/// <summary>
		/// Gets or sets the selected color.
		/// </summary>
		/// <value>The color selected.</value>
		public Color SelectedColor
		{
			get { return (Color)GetValue(SelectedColorProperty); }
			set { SetValue(SelectedColorProperty, value); }
		}
		/// <summary>
		/// SelectedColorProperty changed event handler.
		/// </summary>
		/// <param name="d">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> 
		/// instance containing the event data.</param>
		static void SelectedColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			KnownColorPicker ths = d as KnownColorPicker;
			if (ths == null)
				return;

			Color color = (Color)(e.NewValue);
			ths.viewModel.SelectedColor = color;
			ths.RaiseEvent(new SelectedColorChangedEventArgs(color, SelectedColorChangedEvent, ths));
		}
		#endregion SelectedColor

		#region SelectedColorChangedEvent
		/// <summary>
		/// SelectedColorChangedEvent backing RoutedEvent.
		/// </summary>
		public static readonly RoutedEvent SelectedColorChangedEvent
			= EventManager.RegisterRoutedEvent("SelectedColorChanged", RoutingStrategy.Bubble
				, typeof(SelectedColorChangedEventHandler), typeof(KnownColorPicker));
		/// <summary>
		/// SelectedColorChangedEvent accessors.
		/// </summary>
		public event SelectedColorChangedEventHandler SelectedColorChanged
		{
			add { AddHandler(SelectedColorChangedEvent, value); }
			remove { RemoveHandler(SelectedColorChangedEvent, value); }
		}
		#endregion SelectedColorChangedEvent
	}
}
